package com.ldatum.crm.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ldatum.crm.dao.TScheduleDao;
import com.ldatum.crm.domain.TSchedule;
import com.ldatum.crm.util.HibernateUtil;

public class TScheduleDaoImpl implements TScheduleDao {

	@Override
	public TSchedule findById(Integer id) {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			return (TSchedule) s.get(TSchedule.class, id);
		}finally{
			if(s!=null)
			s.close();
		}
	}

	@Override
	public void save(TSchedule schedule) {
		HibernateUtil.add(schedule);
	}

	@Override
	public void update(TSchedule schedule) {
		HibernateUtil.update(schedule);
	}

	@Override
	public List<TSchedule> list(Class<TSchedule> class1, String creater) {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TSchedule as schedule where createMen like :createMen or executeMen like :executeMen order by serveTime ASC";
			Query q = s.createQuery(hql);
			q.setString("createMen", creater);
			q.setString("executeMen", creater);
			@SuppressWarnings("unchecked")
			List<TSchedule> list = (List<TSchedule>) q.list();
			return list;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}

	@Override
	public List<TSchedule> findUnFinished(String fullname) {
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			String hql = "from TSchedule as schedule where executeMen like :executeMen and status = '未完成' order by serveTime ASC";
			Query q = s.createQuery(hql);
			q.setString("executeMen", fullname);
			@SuppressWarnings("unchecked")
			List<TSchedule> list = (List<TSchedule>) q.list();
			return list;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}

}
